#include "lightbutton.h"
#include "rescolor.h"
#include <QPainter>
#include <QDebug>

LightButton::LightButton(QWidget *parent) : QWidget(parent)
{
    text = "";
    bgColor = QColor(100, 184, 255);
    textColor = QColor(255, 255, 255);
}

void LightButton::paintEvent(QPaintEvent *)
{
    static int nums = 0;
    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    qDebug() << "paintEvent:" << ++nums << endl;

    //绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //绘制矩形区域
    painter.setPen(Qt::NoPen);
    painter.setBrush(bgColor);
    painter.drawRoundedRect(this->rect(), 5, 5);

    //绘制文字
    if (!text.isEmpty()) {
        QFont font;
        font.setPixelSize(side - 20);
        painter.setFont(font);
        painter.setPen(textColor);
        painter.drawText(this->rect(), Qt::AlignCenter, text);
    }
}

QString LightButton::getText() const
{
    return this->text;
}

QColor LightButton::getTextColor() const
{
    return this->textColor;
}

QColor LightButton::getBgColor() const
{
    return this->bgColor;
}

void LightButton::setText(const QString &text)
{
    if (this->text != text) {
        this->text = text;
        this->update();
    }
}

void LightButton::setTextColor(const QColor &textColor)
{
    if (this->textColor != textColor) {
        this->textColor = textColor;
        this->update();
    }
}

void LightButton::setBgColor(const QColor &bgColor)
{
    if (this->bgColor != bgColor) {
        this->bgColor = bgColor;
        this->update();
    }
}

void LightButton::updateBgColor()
{
    switch(bgType) {
    case 0: {
        bgType = 1;
        this->setBgColor(QColor(0xE2, 0xE9, 0xE3));
    } break;
    case 1: {
        bgType = 2;
        this->setBgColor(QColor(0xF1, 0xF1, 0xF5));
    } break;
    case 2: {
        bgType = 0;
        this->setBgColor(QColor(0xD9, 0xDF, 0xE9));
    } break;
    }
}
